import { loginNumber } from "../../api/login";

const users = JSON.parse(localStorage.getItem("user")) || {};
const state = {
  nickName: users.nickName,
  token: users.token,
};
const getters = {};
const actions = {
  async loginTo({ commit }, { phone, password }) {
    let user = await loginNumber({ phone, password });
    commit("LOGINTO", user);
  },
};
const mutations = {
  LOGINTO(state, user) {
    state.nickName = user.nickName;
    state.token = user.token;
    localStorage.setItem(
      "user",
      JSON.stringify({ nickName: user.nickName, token: user.token })
    );
  },
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};
